#include "../Driver_PIC/system.h"
#include "../Driver_PIC/timer0.h"
#include "../Driver_PIC/pic18_i2c.h"
#include "../Driver_PIC/pic18_flash.h"

#include "../Driver_LCD19264/CN_16_16_Index.h"
#include "../Driver_LCD19264/Image.h"
#include "../Driver_LCD19264/ASCII.h"
#include "../Driver_LCD19264/lcd19264.h"

#include "../AT24C/at24c02.h"
typedef union
{
    unsigned int full;
    struct
    {
        unsigned char low;
        unsigned char high;
    };
} inttochar;

extern signed char temp;
signed char temp = 0;

#define LED1 LATCbits.LC5
#define REALY LATCbits.LATC1

void interrupt high_priority SystemServer()
{
    volatile inttochar timer0cal;
    if(INTCONbits.T0IF && INTCON2bits.TMR0IP && INTCONbits.TMR0IE)
    {
        INTCONbits.T0IF = 0 ;
        timer0cal.low = TMR0L;
        timer0cal.high = TMR0H;
        timer0cal.full += Timer0StartVal;
        TMR0H = timer0cal.high ;
        TMR0L = timer0cal.low ;
        Timer0Over ++ ;
        return;
    }
}

void RunInit()
{
    /************************AT24c1 test**************************************/
    TRISCbits.RC1    = 0;
    LATCbits.LC1     = 0;

    ANSELCbits.ANSC5 = 0;
    TRISCbits.RC5    = 0;
    LATCbits.LC5     = 0;

    SystemInit();
    SystemClockUpdate();
    Timer0Init(1);
    I2C1Init(I2CMASTERMODE);
   /**********************************AT24c1 test end*************************/

    /*******************************LCD19264 TEST*****************************/
    /*************************************************************************
    unsigned char HI[]="hello,world!\n";
    ANSELAbits.ANSA0 = 0;
    TRISAbits.RA0    = 0;
    LATAbits.LA0     = 0;
    SystemInit();
    SystemClockUpdate();
    Timer0Init(1);
    ANSELB = 0;
    ANSELC = 0;
    ANSELA = 0;
    LCD19264_Init();
        LCD19264_StringDisp_Anti(0,40,HI);
        //    LCD19264_LineDisp(2 ,90,120,Img1);
        LCD19264_ImgDisp(2 ,48 ,192 ,2 ,Img1);

       //     LCD19264_ImgDisp(2 ,64 ,48 ,48 ,dong);

        LCD19264_Char16_32Disp(4,0,'F');
        LCD19264_Char16_32Disp(4,16,'o');
        LCD19264_Char16_32Disp_Anti(4,32,'r');
        LCD19264_Char16_32Disp_Anti(4,48,'c');
        LCD19264_Char16_32Disp_Anti(4,64,'e');

        LCD19264_CN_Disp(0,160,que);
        LCD19264_CN_Disp(0,176,ren);
        LCD19264_CN_Disp_Anti(0,0,que);
        LCD19264_CN_Disp_Anti(0,16,ren);
        ***************************LCD19264 TEST END**************************/
}

void SystemHandle()
{
    // unsigned char ColMove;           //for lcd19264 test
    // ColMove = 0;                     //for lcd19264 test
       unsigned char atRomNum;
       atRomNum = AT24C01ROMSIZE;
            LED1 = 0;
            REALY = 0;
    while(1)
    {

        if(Timer0Over)
        {
            Timer0Run();
        }
        /*******************************************************************/
        /**/
        if(bPulse_100ms)
        {
            LED1 = !LED1;
            REALY =!REALY;
        }
        /**/
    /******************************test at24c01*****************************/
    /**********************************************************************/
    /*   if(atRomNum)
        {
            if(bPulse_50ms)
                atRomNum --;
                At24cWriteByte(0XA0 ,atRomNum,0);
        }/**/
    At24c_WriteBuff[0] = 0x51 ;
    At24c_WriteBuff[1] = 0x52 ;
    At24c_WriteBuff[2] = 0x53 ;
    At24c_WriteBuff[3] = 0x54 ;
    At24c_WriteBuff[4] = 0x45 ;
    At24c_WriteBuff[5] = 0x46 ;
    At24c_WriteBuff[6] = 0x47 ;
    At24c_WriteBuff[7] = 0x48 ;

 //   At24cWriteBytes(0xA0,8 ,4 ,At24c_WriteBuff); //写一个字符串到EEPROM
    asm("nop");
    asm("nop");

 //   temp = At24cReadBytes(0xA0,0,64,At24c_ReadBuff);
    
    if(temp == -2)
        asm("nop");
    asm("nop");
    asm("nop");
    if(temp == -2)
        asm("nop");
    asm("nop");
    asm("nop");
    /*****************************test at24c01 end*************************/
        
        /**************************LCD19264 TEST******************************/
        /*********************************************************************
       if(bPulse_100ms)
        {
            LATAbits.LA0 = (!LATAbits.LA0);

                LCD19264_ImgDisp(2 ,ColMove++ ,48 ,48 ,dong);
                if(ColMove > 192)
                {
                    ColMove = 0;
                    LCD19264_StringDisp(4,152,"Sky\n");
                    LCD19264_StringDisp(6,152,"Earth\n");
                    LCD19264_Char16_32Disp(4,48,'F');
                    LCD19264_Char16_32Disp(4,64,'o');
                    LCD19264_Char16_32Disp(4,80,'r');
                    LCD19264_Char16_32Disp(4,96,'c');
                    LCD19264_Char16_32Disp(4,112,'e');
                    LCD19264_ImgDisp(2 ,48 ,192 ,2 ,Img1);
                }
        }
        /***************************LCD19264 TEST END ************************/
        ClearPulse();
    }     
}